package com.woniu.hospital.service.impl;

import com.woniu.hospital.domain.Bedinfo;
import com.woniu.hospital.domain.EquipemntUse;
import com.woniu.hospital.domain.Hosptilnews;
import com.woniu.hospital.mapper.BedinfoMapper;
import com.woniu.hospital.mapper.EquipemntUseMapper;
import com.woniu.hospital.mapper.HosptilnewsMapper;
import com.woniu.hospital.service.IDeptService;
import com.woniu.hospital.service.IDoctorService;
import com.woniu.hospital.service.IHosptilnewsService;
import com.woniu.user.domain.Doctor;
import com.woniu.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * 住院信息表Service业务层处理
 * 
 * @author wolf-six
 * @date 2023-02-15
 */
@Service
public class HosptilnewsServiceImpl implements IHosptilnewsService 
{
    @Autowired
    private HosptilnewsMapper hosptilnewsMapper;
    @Autowired
    private BedinfoMapper bedinfoMapper;
    @Autowired
    private EquipemntUseMapper equipemntUseMapper;
    /**
     * 查询住院信息表
     * 
     * @param hosptilId 住院信息表主键
     * @return 住院信息表
     */
    @Override
    public Hosptilnews selectHosptilnewsByHosptilId(Long hosptilId) {
        Hosptilnews hosptilnews = hosptilnewsMapper.selectHosptilnewsByHosptilId(hosptilId);
        return hosptilnews;
    }

    /**
     * 查询住院信息表列表
     * 
     * @param hosptilnews 住院信息表
     * @return 住院信息表
     */
    @Override
    public List<Hosptilnews> selectHosptilnewsList(Hosptilnews hosptilnews)
    {
        return hosptilnewsMapper.selectHosptilnewsList(hosptilnews);
    }

    /**
     * 新增住院信息表
     * 
     * @param hosptilnews 住院信息表
     * @return 结果
     */
    @Override
    @Transactional
    public int insertHosptilnews(Hosptilnews hosptilnews) {
        int min = 10000000;
        int max = 99999999;
        Random random = null;
        try {
            random = SecureRandom.getInstanceStrong();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        assert random != null;
        int a = random.nextInt(max) % (max - min + 1) + min;
        hosptilnews.setHosptilIdcard(a + "");
        hosptilnews.setHosptilStatus("住院中");
        hosptilnews.setHosptilIntime(new Date());

        Bedinfo bedinfo = new Bedinfo();
        bedinfo.setBedId(hosptilnews.getBedId());
        bedinfo.setBedStatus("正在使用");
        bedinfo.setPatientId(hosptilnews.getPatientId());
        bedinfoMapper.updateBedinfo(bedinfo);

        EquipemntUse equipemntUse = new EquipemntUse();
        equipemntUse.setEquipmentuseTime(new Date());
        equipemntUse.setEquipmentId(hosptilnews.getBedId());
        equipemntUse.setDocterId(hosptilnews.getDocterId());
        equipemntUse.setPatientId(hosptilnews.getPatientId());
        equipemntUseMapper.insertEquipemntUse(equipemntUse);
        return hosptilnewsMapper.insertHosptilnews(hosptilnews);
    }

    /**
     * 修改住院信息表
     * 
     * @param hosptilnews 住院信息表
     * @return 结果
     */
    @Override
    public int updateHosptilnews(Hosptilnews hosptilnews)
    {
        return hosptilnewsMapper.updateHosptilnews(hosptilnews);
    }

    /**
     * 批量删除住院信息表
     * 
     * @param hosptilIds 需要删除的住院信息表主键
     * @return 结果
     */
    @Override
    public int deleteHosptilnewsByHosptilIds(Long[] hosptilIds)
    {
        return hosptilnewsMapper.deleteHosptilnewsByHosptilIds(hosptilIds);
    }

    /**
     * 删除住院信息表信息
     * 
     * @param hosptilId 住院信息表主键
     * @return 结果
     */
    @Override
    public int deleteHosptilnewsByHosptilId(Long hosptilId)
    {
        return hosptilnewsMapper.deleteHosptilnewsByHosptilId(hosptilId);
    }

    @Override
    @Transactional
    public int goOut(Long hosptilIds) {
        Bedinfo bedinfo = new Bedinfo();
        Long bedId = hosptilnewsMapper.selectHosptilnewsByHosptilId(hosptilIds).getBedId();
        bedinfo.setBedId(bedId);
        bedinfo.setBedStatus("待使用");
        bedinfo.setPatientId(null);
        bedinfoMapper.updateBedinfo(bedinfo);

        EquipemntUse equipemntUse = new EquipemntUse();
        EquipemntUse equipemntUse1 = equipemntUseMapper.selectByBedId(bedId);
        equipemntUse.setEquipmentuseId(equipemntUse1.getEquipmentuseId());
        Date startTime = equipemntUse1.getEquipmentuseTime();
        Date endTime = new Date();
        equipemntUse.setEquipmentuseReturntime(endTime);
        long s = endTime.getTime() - startTime.getTime();
        TimeUnit time = TimeUnit.DAYS;
        long days = time.convert(s, TimeUnit.MILLISECONDS);
        BigDecimal bigDecimal = new BigDecimal(days);
        BigDecimal bedPrice = bedinfoMapper.selectBedinfoByBedId(bedId).getBedPrice();
        BigDecimal multiply = bedPrice.multiply(bigDecimal);
        equipemntUse.setEquipmentuseMoney(multiply.add(bedPrice));
        equipemntUseMapper.updateEquipemntUse(equipemntUse);

        Hosptilnews hosptilnews = new Hosptilnews();
        hosptilnews.setHosptilId(hosptilIds);
        hosptilnews.setHosptilStatus("已出院");
        hosptilnews.setHosptilOuttime(new Date());

        return hosptilnewsMapper.updateHosptilnews(hosptilnews);
    }
/*
    @Override
    public List<Hosptilnews> selectAll() {
        return hosptilnewsMapper.selectAll();
    }*/
}
